﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace BookOnline.Data
{
    public class CatController : SqlDataProvider
    {
        public List<Data.Cat> getAll()
        {
            List<Data.Cat> list = new List<Data.Cat>();
            using (SqlCommand cmd = new SqlCommand("CategorySelectAll", getConnect()))
            {
                Cat obj = new Cat();
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.CatIDataReader(dr));
                    }
                }
                dr.Close();
                obj = null;
            }
            return list;
        }

        public List<Data.Cat> getByID(string id)
        {
            List<Data.Cat> list = new List<Data.Cat>();
            using (SqlCommand cmd = new SqlCommand("CategorySelect", getConnect()))
            {
                Cat obj = new Cat();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@CatID",id));
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.CatIDataReader(dr));
                    }
                }
                dr.Close();
                obj = null;
            }
            return list;
        }

        public bool Cat_Add(Cat data)
        {
            int i = 0;
            using (SqlCommand cmd = new SqlCommand("CategoryInsert", getConnect()))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@CatName", data.CatName));
                cmd.Parameters.Add(new SqlParameter("@CatParent", data.CatParent));
                i = cmd.ExecuteNonQuery();
            }
            if (i == 0) { return false; }
            else
                return true;
        }
        public bool Cat_Update(Cat data)
        {
            int i = 0;
            using (SqlCommand cmd = new SqlCommand("CategoryUpdate", getConnect()))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@CatID", data.CatID));
                cmd.Parameters.Add(new SqlParameter("@CatParent", data.CatParent));
                cmd.Parameters.Add(new SqlParameter("@CatName", data.CatName));
                i = cmd.ExecuteNonQuery();
            }
            if (i == 0) { return false; }
            else
                return true;
        }

        public bool Cat_Delete(string id)
        {
            int i = 0;
            using (SqlCommand cmd = new SqlCommand("CategoryDelete", getConnect()))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@CatID", id));
                i = cmd.ExecuteNonQuery();
            }
            if (i == 0) { return false; }
            else
                return true;
        }
    }
}
